home *** CD-ROM | disk | FTP | other *** search
/ Dr. Windows 3 / dr win3.zip / dr win3 / WINSOUND / CHIMES.ZIP / MAIN.CPP < prev    next >
C/C++ Source or Header  |  1993-08-10  |  18KB  |  1,046 lines

  1. #include "main.h"
  2.  
  3. static char *Profile = CHIMEINI;
  4. static char *Version = CHIMEVER;
  5.  
  6. CMyMainDialog::CMyMainDialog(CWnd *parentWnd)
  7. {
  8.     RECT dr;
  9.     RECT pcr;
  10.     RECT pwr;
  11.     
  12.     p_gotProfile = FALSE;
  13.     
  14.     Create("main", parentWnd);
  15.     
  16.     GetClientRect(&dr);
  17.     parentWnd->GetWindowRect(&pwr);
  18.     parentWnd->GetClientRect(&pcr);
  19.     
  20.     pwr.right += dr.right - (pcr.right - pcr.left);
  21.     pwr.bottom += dr.bottom - (pcr.bottom - pcr.top);
  22.     
  23.     parentWnd->MoveWindow(&pwr);
  24.         
  25. }
  26.  
  27. void
  28. CMyMainDialog::cleanup()
  29. {
  30.     // close MIDI
  31.     if (p_hMidi) {
  32.         midiOutClose(p_hMidi);
  33.     }
  34.     
  35.     if (p_gotProfile) {
  36.         saveDefaults();
  37.     }
  38. }
  39.  
  40. void
  41. CMyMainDialog::saveDefaults()
  42. {
  43.     char *buf = new char[30];
  44.     char *buf2 = new char[30];
  45.     int i;
  46.     
  47.     wsprintf(buf, "%d", p_channel + 1);
  48.     WritePrivateProfileString(Version, "channel",
  49.         buf, Profile);
  50.         
  51.     for (i = 0; i < 16; i++) {
  52.         wsprintf(buf, "program%d", i + 1);
  53.         wsprintf(buf2, "%d", p_program[i]);
  54.         WritePrivateProfileString(Version,
  55.             buf, buf2, Profile);
  56.     }
  57.     wsprintf(buf, "%d", p_minVol);
  58.     WritePrivateProfileString(Version, "minvol", buf, Profile);
  59.     wsprintf(buf, "%d", p_maxVol);
  60.     WritePrivateProfileString(Version, "maxvol", buf, Profile);
  61.     
  62.     wsprintf(buf, "%d", p_minLength);
  63.     WritePrivateProfileString(Version, "minlength", buf, Profile);
  64.     wsprintf(buf, "%d", p_maxLength);
  65.     WritePrivateProfileString(Version, "maxlength", buf, Profile);
  66.  
  67.     wsprintf(buf, "%d", p_lowNote);
  68.     WritePrivateProfileString(Version, "lownote", buf, Profile);
  69.     wsprintf(buf, "%d", p_highNote);
  70.     WritePrivateProfileString(Version, "highnote", buf, Profile);
  71.     
  72.     wsprintf(buf, "%d", p_restProb);
  73.     WritePrivateProfileString(Version, "rest", buf, Profile);
  74.     
  75.     for (i = 0; i < 128; i++) {
  76.         wsprintf(buf, "note%d", i + 1);
  77.         wsprintf(buf2, "%d", p_noteProb[i]);
  78.         WritePrivateProfileString(Version, buf, buf2, Profile);
  79.     }
  80.             
  81.     delete buf;
  82.     delete buf2;
  83. }
  84.  
  85. BOOL
  86. CMyMainDialog::OnInitDialog()
  87. {
  88.     UINT merr;
  89.     int i;
  90.     char *buf = new char[30];
  91.  
  92.     CDialog::OnInitDialog();
  93.     
  94.     p_playing = FALSE;
  95.     p_note = -1;
  96.     
  97.     for (i = 0; i < 16; i++) {
  98.         wsprintf(buf, "program%d", i + 1);
  99.         p_program[i] = GetPrivateProfileInt(Version, buf, -1,
  100.             Profile);
  101.     }
  102.     
  103.     p_channel = GetPrivateProfileInt(Version, "channel", 1, Profile) - 1;
  104.     p_lastChannel = 255;    
  105.     channelSB()->SetScrollRange(0, 15);
  106.     channelChange(-1);
  107.  
  108.     programSB()->SetScrollRange(-1, 127);
  109.     programChange(p_program[p_channel]);
  110.     
  111.     p_minVol = GetPrivateProfileInt(Version, "minvol", 90, Profile);
  112.     p_maxVol = GetPrivateProfileInt(Version, "maxvol", 120, Profile);
  113.     minvolSB()->SetScrollRange(0, 127);
  114.     minVolChange(p_minVol, FALSE);
  115.     
  116.     maxvolSB()->SetScrollRange(0, 127);
  117.     maxVolChange(p_maxVol, FALSE);
  118.     
  119.     p_minLength = GetPrivateProfileInt(Version, "minlength", 100, Profile);
  120.     p_maxLength = GetPrivateProfileInt(Version, "maxlength", 500, Profile);
  121.     minlenSB()->SetScrollRange(1, 3000);
  122.     minlenSB()->SetScrollPos(p_minLength);
  123.     minLengthChange(p_minLength, FALSE);
  124.         
  125.     maxlenSB()->SetScrollRange(1, 3000);
  126.     maxlenSB()->SetScrollPos(p_maxLength);
  127.     maxLengthChange(p_maxLength, FALSE);
  128.     
  129.     p_lowNote = GetPrivateProfileInt(Version, "lownote", DMN_C_3, Profile);
  130.     p_highNote = GetPrivateProfileInt(Version, "highnote", DMN_C_6, Profile);
  131.     lownoteSB()->SetScrollRange(DMN_C_0, DMN_G_10);
  132.     lownoteSB()->SetScrollPos(p_lowNote);
  133.     lowNoteChange(p_lowNote, FALSE);
  134.     
  135.     highnoteSB()->SetScrollRange(DMN_C_0, DMN_G_10);
  136.     highnoteSB()->SetScrollPos(p_highNote);
  137.     highNoteChange(p_highNote, FALSE);
  138.     
  139.     p_restProb = GetPrivateProfileInt(Version, "rest", 10, Profile);
  140.     restSB()->SetScrollRange(0, 100);
  141.     restChange(p_restProb);
  142.  
  143.     merr = midiOutOpen(&p_hMidi, MIDIMAPPER, (DWORD)(UINT)m_hWnd, 0, CALLBACK_WINDOW);
  144.  
  145.     if (merr) {
  146.         char mmesg[80];
  147.         midiOutGetErrorText(merr, mmesg, sizeof(mmesg));
  148.         MessageBox(mmesg, "MIDI Mapper",
  149.             MB_OK | MB_ICONSTOP);
  150.         PostQuitMessage(0);
  151.     }
  152.  
  153.     srand((unsigned)time(NULL));    
  154.     
  155.     for (i = 0; i< 128 ; i++) {
  156.         wsprintf(buf, "note%d", i + 1);
  157.         p_noteProb[i] = GetPrivateProfileInt(Version, buf, 50, Profile);
  158.     }
  159.     
  160.     p_gotProfile = TRUE;
  161.     
  162.     delete buf;
  163.  
  164.     return TRUE;
  165. }
  166.  
  167. void
  168. CMyMainDialog::OnPlayStop()
  169. {
  170.     playstop();
  171. }
  172.  
  173. void
  174. CMyMainDialog::playstop()
  175. {
  176.     if (p_playing) {
  177.         playPB()->SetWindowText("Play");
  178.         p_playing = FALSE;
  179.     } else {
  180.         playPB()->SetWindowText("Stop");
  181.         p_playing = TRUE;
  182.         program();
  183.         nextNote();
  184.     }
  185. }
  186.  
  187. //
  188. // program
  189. //
  190.  
  191. void
  192. CMyMainDialog::program()
  193. {
  194.  
  195.     if (p_program[p_channel] >= 0 && p_program[p_channel] <= 127) {
  196.         dmidiSendChannelMessage(p_hMidi, p_channel,
  197.             DMM_PROGRAM, (BYTE)(p_program[p_channel]), 0);
  198.     }
  199. }
  200.  
  201. BYTE
  202. CMyMainDialog::getVolume()
  203. {
  204.     int range;
  205.     
  206.     range = (p_maxVol - p_minVol) + 1;
  207.     
  208.     return (p_minVol + (rand() % range));
  209. }
  210.  
  211. UINT
  212. CMyMainDialog::getLength()
  213. {
  214.     int range;
  215.     
  216.     range = (p_maxLength - p_minLength) + 1;
  217.     
  218.     return (p_minLength + (rand() % range));
  219. }
  220.  
  221. int
  222. CMyMainDialog::getNote()
  223. {
  224.     int range;
  225.     int rnum;
  226.     int i;
  227.     long sum;
  228.     
  229.     rnum = rand();
  230.     
  231.     if ((WORD)(rnum % 100) < p_restProb) {
  232.         return -1;   // rest
  233.     }
  234.     
  235.     range = p_highNote - p_lowNote;
  236.     
  237.     // sum the probabilities
  238.     
  239.     sum = 0;
  240.     for (i = p_lowNote; i < (int)p_highNote; i++) {
  241.         sum += p_noteProb[i];
  242.     }
  243.     
  244.     if (sum == 0) {
  245.         return -1;    // rest
  246.     }
  247.     
  248.     // Now, find the slot that has that probability
  249.     
  250.     rnum %= sum;
  251.     i = p_lowNote;
  252.     while (rnum > 0) {
  253.         rnum -= p_noteProb[i];
  254.         i++;
  255.     }
  256.  
  257.     return i;
  258.  
  259. }
  260.  
  261. //
  262. // nextNote
  263. //
  264.  
  265. void
  266. CMyMainDialog::nextNote()
  267. {
  268.     BYTE volume;
  269.     UINT length;
  270.     
  271.     p_note = getNote();
  272.     volume = getVolume();
  273.     length = getLength();
  274.     
  275.     if (p_note >= 0) {
  276.         dmidiSendChannelMessage(p_hMidi, p_channel,
  277.             DMM_NOTE_ON, p_note, volume);
  278.     }
  279.     SetTimer(99, length, NULL);
  280. }
  281.  
  282. void
  283. CMyMainDialog::OnTimer(UINT)
  284. {
  285.     BYTE sChannel;
  286.  
  287.     if (p_note >= 0) {
  288.         if (p_lastChannel != 255) {
  289.             sChannel = p_lastChannel;
  290.             p_lastChannel = 255;
  291.         } else {
  292.             sChannel = p_channel;
  293.         }
  294.         dmidiSendChannelMessage(p_hMidi, sChannel,
  295.             DMM_NOTE_OFF, p_note, 0);
  296.     }
  297.     if (p_playing) {
  298.         nextNote();
  299.     }
  300. }
  301.  
  302. BOOL
  303. CMyMainDialog::shifted()
  304. {
  305.     int ks;
  306.     
  307.     ks = GetKeyState(VK_SHIFT);
  308.     if ((ks & 0x8000) != 0) {
  309.         return TRUE;
  310.     }
  311.     return FALSE;
  312. }
  313.  
  314. void
  315. CMyMainDialog::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar *pSb)
  316. {
  317.     int minv, maxv, value;
  318.     BOOL gang = shifted();
  319.     
  320.     pSb->GetScrollRange(&minv, &maxv);
  321.     value = pSb->GetScrollPos();
  322.     
  323.     switch (nSBCode) {
  324.     
  325.     case SB_LINELEFT:
  326.         if (value > minv) {
  327.             value--;
  328.         }
  329.         break;
  330.         
  331.     case SB_LINERIGHT:
  332.         if (value < maxv) {
  333.             value++;
  334.         }
  335.         break;
  336.         
  337.     case SB_LEFT:
  338.         value = minv;
  339.         break;
  340.         
  341.     case SB_RIGHT:
  342.         value = maxv;
  343.         break;
  344.         
  345.     case SB_PAGELEFT:
  346.         value -= (maxv - minv) / 10;
  347.         if (value < minv) {
  348.             value = minv;
  349.         }
  350.         break;
  351.         
  352.     case SB_PAGERIGHT:
  353.         value += (maxv - minv) / 10;
  354.         if (value > maxv) {
  355.             value = maxv;
  356.         }
  357.         break;
  358.         
  359.     case SB_THUMBPOSITION:
  360.         value = nPos;
  361.         break;
  362.         
  363.     default:
  364.         return;
  365.     }
  366.     
  367.     // handle the operation
  368.     
  369.     if (pSb == channelSB()) {
  370.         channelChange(value);
  371.         return;
  372.     }
  373.     
  374.     if (pSb == programSB()) {
  375.         programChange(value);
  376.         return;
  377.     }
  378.     
  379.     if (pSb == minvolSB()) {
  380.         minVolChange(value, gang);
  381.         return;
  382.     }
  383.     
  384.     if (pSb == maxvolSB()) {
  385.         maxVolChange(value, gang);
  386.         return;
  387.     }
  388.     
  389.     if (pSb == minlenSB()) {
  390.         minLengthChange(value, gang);
  391.         return;
  392.     }
  393.         
  394.     if (pSb == maxlenSB()) {
  395.         maxLengthChange(value, gang);
  396.         return;
  397.     }
  398.     
  399.     if (pSb == lownoteSB()) {
  400.         lowNoteChange(value, gang);
  401.         return;
  402.     }
  403.     
  404.     if (pSb == highnoteSB()) {
  405.         highNoteChange(value, gang);
  406.         return;
  407.     }
  408.     
  409.     if (pSb == restSB()) {
  410.         restChange(value);
  411.         return;
  412.     }
  413. }
  414.  
  415. void
  416. CMyMainDialog::channelChange(int val)
  417. {
  418.     char *buf = new char[30];
  419.     
  420.     if (val >= 0) {
  421.         if (p_lastChannel == 255) {
  422.             p_lastChannel = p_channel;
  423.         }
  424.         p_channel = val;
  425.     }
  426.  
  427.     wsprintf(buf, "%d", p_channel + 1);
  428.     channelST()->SetWindowText(buf);
  429.     
  430.     channelSB()->SetScrollPos(p_channel);
  431.     
  432.     programChange(p_program[p_channel]);
  433.  
  434.     delete buf;
  435. }
  436.  
  437. void
  438. CMyMainDialog::programChange(i